/**
 * Created by fdg282 on 2017/8/4.
 */
$(function(){
    //图片加载
    var static_bath = $('#static_bath').val();
    $("img").lazyload({
        effect: "fadeIn",
        threshold :500,
        placeholder : static_bath + "/static/pc/image/iconPng/loadingShow.png",
        failurelimit : 80,
        skip_invisible : false
    });
    //IE9下兼容placeholder
    if(navigator.userAgent.indexOf("MSIE 9.0")>0) {
        $('[placeholder]').not("input[type='password']").focus(function () {
            var input = jQuery(this);
            if (input.val() == input.attr('placeholder')) {
                input.val('');
                input.removeClass('placeholder');
            }
        }).blur(function () {
            var input = $(this);
            if (input.val() == '' || input.val() == input.attr('placeholder')) {
                input.addClass('placeholder');
                input.val(input.attr('placeholder'));
            }
        }).blur().parents('form').submit(function () {
            $(this).find('[placeholder]').each(function () {
                var input = $(this);
                if (input.val() == input.attr('placeholder')) {
                    input.val('');
                }
            })
        });
    }
//新闻滚动
    function newsScroll(){
        var _newsBow = $(".mini_top_news_list");
        _newsBow.animate({"margin-top":"-2.5rem"},function(){
            $(".mini_top_news_list li:eq(0)").appendTo(_newsBow);
            _newsBow.css({"margin-top":"0"})
        });
    }
    var time = setInterval(newsScroll ,3000);
    $(".mini_top_news_list li").mouseover(function () {
        clearTimeout(time)
    }).mouseout(function () {
        time = setInterval(newsScroll ,3000)
    });
//nav显示隐藏
    $(".nav .js_show_menu").hover(function(){
        var _index = $(this).index();
        $(this).find(".nav_item_box").show();
    },function () {
        $(this).find(".nav_item_box").hide();
    });

    //回到顶部
    /*mod FDG416 2018/1/19 ID471 【商城需求】三张小图正序循环轮播 start */ 
    $(window).scroll(function(){
        if($(this).scrollTop()>=580){
            $(".bg,#udesk_container").fadeIn(800);
        }else{
            $(".bg,#udesk_container").fadeOut(300)
        }
    });
    var bgTop = new Swiper('#bgTop', {
        direction: 'horizontal',
        autoplay:3000,
        autoplayDisableOnInteraction:false,//是否禁止autoplay
        effect : 'slide',
        loop:true,
        observer:true,//修改swiper自己或子元素时，自动初始化swiper 
        observeParents:true,//修改swiper的父元素时，自动初始化swiper 
    });
    /*mod FDG416 2018/1/19 ID471 【商城需求】三张小图正序循环轮播 end */ 
    $(".bg").find(".d2").click(function(){
        // document.documentElement.scrollTop = document.body.scrollTop = 0
        $('body,html').animate({scrollTop: 0}, 800);
    });

    function myHandler(){
        $("#udesk_btn a").click();
    }
    $(".d1").on("click", {}, myHandler);




//搜索
    var $search = $('#search');
    var $searchList = $('#searchList');
    $search.hideseek({
    	nodata: 'No results found',
        navigation: true
    });
    $search.on('keyup',function(event){
        event.p
        var search_name = $(this).val();
        var search_backup = $('#search_backup').val();
        if(search_backup == search_name){
        	return false;
        }
        $.post('/site/get-key-word',{search_name:search_name},success,'json');
        return false;
        function success(data){
            if(data.code == 200){
            	$('#search_backup').val(search_name);
                var html = '';
                var result = data.val;
                if(result.length > 0){
                    result.forEach(function(e){
                        html += '<li class="item"><a href="javascript:; ">'+e.name+'</a></li>';
                    });
                }
                $searchList.html(html);
                var $searchItem = $searchList.children(".item");
                /*mod FDG416 2018/1/17 ID447 【商城需求】IE9以上版本做兼容调整 start*/ 
                var sVal = function(val,name){
                    if(val!==''){
                        name = 'keyword';
                /*mod FDG416 2018/1/17 ID447 【商城需求】IE9以上版本做兼容调整 end*/ 
                        localStorage.setItem("searchVal", val);
                        val = encodeURIComponent(val);
                        window.location.href = '/goods/search?'+name+'=' + val;
                    }
                };
                $searchItem.each(function(){
                    $(this).on('click',function () {
                        var itemText = $(this).text();
                        sVal(itemText)
                    })
                });
                $search.keydown(function(event){
                    if (event.keyCode == 13) {
                    	if($searchList.find('.selected').length > 0){
                    		var inVal = $searchList.find('.selected').children('a').text();
                    	}else{
                    		var inVal = $.trim($search.val().toString());
                    	}
                        //sVal(inVal,'goods_name_cn');
                        sVal(inVal);
                        if($searchItem.length>0){
                            var selectVal  =$searchList.find('.selected').text();
                            $('#search').val(selectVal)
                        }
                    }
                });
                $(".js_icon_search").on('click',function () {
                    var inVal = $.trim($search.val().toString());
                    sVal(inVal);
                    if($searchItem.length>0){
                        var selectVal  =$searchList.find('.selected').text();
                        $('#search').val(selectVal)
                    }
                });
            }
        }
    });

    $(".footer-email").append("<span class='info'></span>");

//底部邮箱报错时
    $('.footer-email .email_value_footer').keyup(function(){
        if($(this).is(".infowaring")){
            $(this).parents('.footer-email').removeClass('f_e_error');
            $(".footer_cheked").fadeIn(200);
            $('.footer-email .inpdel').click(function(){
                $('.footer-email .email_value_footer').removeClass('infowaring');
                $(".footer_cheked").hide();
            });
        }else{
            $(".footer_cheked").hide();
            $(this).parents('.footer-email').addClass('f_e_error').fadeIn(100)
        }
    });
    
    $('.footer-email .email_value_footer').keyup(function(){
        var emaVal = $(this).val();
        if(emaVal!==""&&(/^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$/.test(emaVal))){
            $(".footer-email .info").html('');
            $(this).addClass("infowaring");
        }else{
            $(".footer-email .info").html("邮箱格式不正确").addClass("infoerror");
            $(this).removeClass("infowaring");
        }
    });
    
    
    $('#checkmail').on('click',function(){
        if($('#checkmail').is(':checked')){
            $(".footer_cheked .tipinfo").html('');
        }else{
            $(".footer_cheked .tipinfo").html('请勾选同意《隐私条款》');
        }
    });
    $('.js_f_email').click(function(event){
        event.preventDefault();
        $(this).attr('disabled',true);
        var emailVal = $('.footer-email .email_value_footer').val();
        if($('#checkmail').is(':checked')){
            $(".footer_cheked .tipinfo").html('');
            $.post('/common/subscription',{email:emailVal},success,'json');
            return false;
            function success(data){
                if(data.code == 500){
                    $(".footer_cheked").hide();
                    $(".email_value_footer").parents('.footer-email').addClass('f_e_error').fadeIn(100);
                    $(".footer-email .info").html(data.val).addClass("infoerror");
                    $('.js_f_email').attr('disabled',false);
                }else{
                    $(".footer_cheked").hide();
                    $(".email_value_footer").parents('.footer-email').removeClass('f_e_error').fadeIn(100);
                    $(".footer-email .info").html("").removeClass("infoerror");
                    $('.js_f_email').attr('disabled',false);
                    $.layerShow("","你已成功订阅最新资讯");
                    $('.footer-email .email_value_footer').val('')

                }
            }
        }else{
            $(".footer_cheked .tipinfo").html('请勾选同意《隐私条款》');
            $('.js_f_email').attr('disabled',false);
        }
    });

    $('.footer-email .email_value_footer').keyup(function(){
        var emaVal = $(this).val();
        if(emaVal!==""&&(/^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$/.test(emaVal))){
            $(".footer-email .info").html('');
            $(this).addClass("infowaring");
        }else{
            $(".footer-email .info").html("邮箱格式不正确").addClass("infoerror");
            $(this).removeClass("infowaring");
        }
    });

    $('.footer-email .email_value_footer').focus(function(){
        if($(this).parents(".input-box").find('span.inpdel').length<=0){
            $(this).parents(".input-box").append('<span class="inpdel">X</span>');
        }
        $(".inpdel").on('click',function(){
            $(this).siblings('input').val('').focus();
        });
    });

    //退出登录
    $('#master_logout').click(function () {
        $.post('/login/logout', {}, function success(data) {
            if (data.code) {
                window.location.href = data.url;
            } else {
                window.location.href = data.url;
            }
            //add by allen.qiang 20170923 退出后清空（网站日志用） start
            $.cookie('user_name', null, {path: '/', expires: 30});
            //add by allen.qiang 20170923 退出后清空（网站日志用） end
        }, 'json');
    });
});



/* 鼠标右键禁用复制图片 */ 
function clicknm(e) {
    if (document.all) {
        if (event.button==1 || event.button==2 || event.button==3) {
            oncontextmenu='return false';
        }
    }
    if (document.layers) {
        if (e.which == 3) {
            oncontextmenu='return false';
        }
    }
}
if (document.layers) {
    document.captureEvents(Event.MOUSEDOWN);
}
document.onmousedown= clicknm;
document.oncontextmenu = new Function("return false;")